package com.run.cat.common.base;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;

import com.run.cat.common.enums.BackResultEnum;
import com.run.cat.common.enums.ResultEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @CreateDate: 2018/08/9 16:29
 * @author: zhangcg
 * @since: JDK 1.8
 * @Description: 返回实体类
 */
@Slf4j
public class BaseResult implements Serializable {


	private static final long serialVersionUID = 1L;
	/** 成功 */
	public static final String SUCCESS = ResultEnum.SUCCESS.getCode();
	/** 成功信息 */
	public static final String SUCCESS_MESSAGE = ResultEnum.SUCCESS.getMsg();
	/** 失败 */
	public static final String FAIL = ResultEnum.ERROR.getCode();
	/** 失败信息 */
	public static final String FAIL_MESSAGE = ResultEnum.ERROR.getMsg();
	/** 返回状态码 */
	private String code;
	/** 返回状态码 */
	private String message;
    /** 返回数据 */
	private Object data;

	public String getCode() {
		return code;
	}

	public BaseResult setCode(String code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public BaseResult setMessage(String message) {
		this.message = message;
		return this;
	}

	public Object getData() {
		return data;
	}

	public BaseResult setObj(Object data) {
		this.data = data;
		return this;
	}

	public BaseResult(String code, String message, Object data) {
		super();
		this.code = code;
		this.message = message;
		this.data = data;
		if(!SUCCESS.equals(code)){
			log.info("返回结果日志 end -> [{}]",toString());
		}

	}

	public BaseResult(String code, String message) {
		this.code = code;
		this.message = message;
		if(!SUCCESS.equals(code)){
			log.info("返回结果日志 end -> [{}]",toString());
		}
	}

	public BaseResult() {
		super();
	}

	public BaseResult(BackResultEnum backResultEnum) {
		super();
		this.code = backResultEnum.getCode();
		this.message = backResultEnum.getMsg();
	}

	public static BaseResult success() {
		return new BaseResult(ResultEnum.SUCCESS.getCode(), SUCCESS_MESSAGE,  new HashMap<>());
	}

	public static BaseResult successMsg(String msg) {
		return new BaseResult(SUCCESS, msg,  new HashMap<>());
	}

	public static BaseResult successData(String key, Object data) {
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put(key,data);
		return new BaseResult(SUCCESS, SUCCESS_MESSAGE, resultMap);
	}

	public static BaseResult successData(Object data) {
		return new BaseResult(SUCCESS, SUCCESS_MESSAGE, data);
	}

	public static BaseResult successMsgAndData(String msg, Object data) {
		return new BaseResult(SUCCESS, msg, data);
	}

	public static BaseResult fail() {
		return new BaseResult(FAIL, FAIL_MESSAGE,  new HashMap<>());
	}

	public static BaseResult failMsg(String msg) {
		return new BaseResult(FAIL, msg,  new HashMap<>());
	}

	public static BaseResult failCodeAndMsg(String code, String msg) {
		return new BaseResult(code, msg, new HashMap<>());
	}

	public static BaseResult failMsgAndData(String msg, Object data) {
		return new BaseResult(FAIL, msg, data);
	}

	public static BaseResult failCodeMsgAndData(String code, String msg, Object data) {
		return new BaseResult(code, msg, data);
	}

	public static BaseResult failCodeMsg(String code, String msg) {
		return new BaseResult(code, msg,  new HashMap<>());
	}

	public boolean resultValid() {
		return this.getCode().equals(SUCCESS);
	}

	public static BaseResult result(BackResultEnum backResultEnum) {
		return new BaseResult(backResultEnum.getCode(), backResultEnum.getMsg(), new HashMap<>());
	}

	public static BaseResult failCodeMsg(ResultEnum resultEnum) {
		return new BaseResult(resultEnum.getCode(), resultEnum.getMsg(), new HashMap<>());
	}
	public static BaseResult successCodeMsg(ResultEnum resultEnum) {
		return new BaseResult(resultEnum.getCode(), resultEnum.getMsg(), new HashMap<>());
	}
	public String toString(){
		return JSON.toJSONString(this);
	}

	/**
	 * 成功
	 */
	@JsonIgnore
	public boolean isSuccess() {
		return SUCCESS.equals(this.code);
	}

	/**
	 * 失败
	 */
	@JsonIgnore
	public boolean isFail() {
		return !isSuccess();
	}

}
